package info.negies.youhao.test.action;

import info.negies.youhao.test.bean.Time;

import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.Map;
import java.util.logging.Logger;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class TimeAction extends ActionSupport implements Action{
	/**
	 * 
	 */
	private static final long serialVersionUID = 7606442286491164895L;

	private static final Logger log = Logger.getLogger(TimeAction.class.getName());

	private Time time;
	public Time getTime() {
		return time;
	}
	public void setTime(Time time) {
		this.time = time;
	}
	public String execute() {

		ActionContext context = ActionContext.getContext();
		Map request = (Map) context.get("request");
		
		Calendar c1 = Calendar.getInstance();
		Calendar c2 = Calendar.getInstance();
		
		c1.set(Calendar.HOUR_OF_DAY,Integer.parseInt(time.getTime1().substring(0,2)));
		c1.set(Calendar.MINUTE,Integer.parseInt(time.getTime1().substring(2,4)));
		c1.set(Calendar.SECOND,Integer.parseInt(time.getTime1().substring(4)));
		c1.set(Calendar.MILLISECOND,0);

		c2.set(Calendar.HOUR_OF_DAY,Integer.parseInt(time.getTime2().substring(0,2)));
		c2.set(Calendar.MINUTE,Integer.parseInt(time.getTime2().substring(2,4)));
		c2.set(Calendar.SECOND,Integer.parseInt(time.getTime2().substring(4)));
		c2.set(Calendar.MILLISECOND,0);

		long millseconds = c2.getTimeInMillis() - c1.getTimeInMillis();

		Date d = new Date(millseconds);
		System.out.println(d);
		System.out.println((new java.text.SimpleDateFormat("hh:mm:ss")).format(d));
		context.put("result", (new java.text.SimpleDateFormat("HH:mm:ss")).format(d));

		return SUCCESS;
	}
}
